const db=require('../db/index')
const bcypt=require('bcryptjs')

exports.getUserInfo=(req,res)=>{
    console.log(req.auth,'req>>>>>');
    const sqlStr='select * from user_tb where id=?'
    db.query(sqlStr,req.auth.id,(err,result)=>{
        if(err) return res.interRes('出错啦','9999')
        if(result.length!==1){
            return res.interRes('用户信息查询失败','9999') 
        }
        res.send({
            status:'200',
            msg:"用户信息查询成功",
            data:result[0]
        })
    })
}

exports.updateUser=(req,res)=>{
    const sqlStr='update user_tb set ? where id=?'
    db.query(sqlStr,[req.body,req.body.id],(err,result)=>{
        if(err) return res.interRes('出错啦')
        if(result.affectedRows!==1){
            return res.interRes('更新用户信息失败')
        }
        res.send({
            status:'200',
            msg:'更新用户信息成功'
        })
    })
}
exports.updatePwd=(req,res)=>{
    const sqStr='select * from user_tb where id=?'
    db.query(sqStr,req.auth.id,(err,result)=>{
        if(err) return res.interRes('出错啦')
        if(result.length!==1){
            return res.interRes('获取用户信息失败')
        }
        const compareResult=bcypt.compareSync(req.body.oldpwd,result[0].password)
        if(!compareResult)return res.interRes('用户旧密码不符')
        const newpwd=bcypt.hashSync(req.body.newpwd,5)
        
    const sqStr2='update user_tb set password=? where id=?'
    db.query(sqStr2,[newpwd,req.auth.id],(err,result)=>{
        if(err) return res.interRes('出错啦')
        if(result.affectedRows!==1){
            return res.interRes('更新密码失败')
        }
        res.send({
            status:'200',
            msg:'密码更新成功'
        })
        })
    })
}